package com.example.smallshop;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.smallshop.database.DBUser;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BlankFragment_me extends Fragment {


    public BlankFragment_me() {
        // Required empty public constructor
    }


    public static BlankFragment_me newInstance( ) {
        BlankFragment_me fragment = new BlankFragment_me();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    TextView myname;
    LinearLayout myrecord,change_address,myinfo,about,contact;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_blank_me, container, false);
        myrecord=v.findViewById(R.id.myrecord);
        myname=v.findViewById(R.id.myname);

        change_address=v.findViewById(R.id.change_address);
        myinfo=v.findViewById(R.id.myinfo);
        about=v.findViewById(R.id.about);
        contact=v.findViewById(R.id.contact);

        myname.setText("欢迎您！亲爱的"+Login.user.getName());
Myonclick myonclick=new Myonclick();
        about.setOnClickListener(myonclick);
        change_address.setOnClickListener(myonclick);
        myrecord.setOnClickListener(myonclick);
        contact.setOnClickListener(myonclick);
        myinfo.setOnClickListener(myonclick);


        return v;
    }
    class Myonclick implements View.OnClickListener {
        @Override
       public void onClick(View v) {
            switch (v.getId()) {
                case R.id.myrecord:
                    //Toast.makeText(getContext(), "record", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), MyRecord.class);
                    startActivity(intent);
                    break;
                case R.id.contact:
                    joinQQ();
                    break;
                case R.id.myinfo:
                    Intent intent1 = new Intent(getActivity(), MyInfo.class);
                    startActivity(intent1);
                    break;
                case R.id.change_address:
                    changeAddress();
                    break;
                case R.id.about:
                    Intent intent3 = new Intent(getActivity(), About.class);
                    startActivity(intent3);
                    break;
            }
        }
    }
    //修改收货地址
    void changeAddress(){
        View view= LayoutInflater.from(getActivity() )
                .inflate(R.layout.layout_change_address, null);
        AlertDialog.Builder customBuilder= new AlertDialog.Builder(getContext())
                .setTitle("修改收货地址")
                .setView(view) ;
        final AlertDialog dialog= customBuilder.create();
        dialog.show();
        //自定义按钮并注册监听器
        final EditText ed_number=view.findViewById(R.id.ed_number);

        ed_number.setHint(Login.user.getAddress());
        Button btn_ok=view.findViewById(R.id.btn_ok);
        Button btn_cancel=view.findViewById(R.id.btn_cancel);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(), ed_number.getText().toString(), Toast.LENGTH_LONG).show();
                DBUser db=new DBUser(getActivity());
                Login.user.setAddress(ed_number.getText().toString());
                if(db.change(Login.user)){
                    Toast.makeText(getContext(), "修改成功！", Toast.LENGTH_SHORT).show();

                }else{
                    Toast.makeText(getContext(), "修改失败！", Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    void joinQQ() {
        try {
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + "1900232822";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "请检查是否安装了腾讯QQ", Toast.LENGTH_SHORT).show();
        }
    }
}